﻿using System.Collections.Generic;
using UnityEngine;
using System.Collections;

namespace Inlycat
{
    /// <summary>
    /// 窗口管理
    /// </summary>

    public class PopWinManager
    {
        /// <summary>
        /// 已打开的窗口
        /// </summary>
        private static Dictionary<PopWinName, PopWin> _openedWinDic;

        /// <summary>
        /// 已打开的窗口
        /// </summary>
        public static Dictionary<PopWinName, PopWin> OpenedWinDic
        {
            get { return _openedWinDic = _openedWinDic ?? new Dictionary<PopWinName, PopWin>(); }
        }

        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="name">窗口名</param>
        /// <param name="closeOthers">是否关闭其他</param>
        /// <param name="data">数据</param>
        public static void Open(PopWinName name, bool closeOthers = true, object data = null)
        {
            if (closeOthers)
            {
                CloseAll();
            }

            PopWin win = null;
            if (!OpenedWinDic.ContainsKey(name))
            {
                win = InitPopWin(name);
                OpenedWinDic.Add(name, win);
            }

            if (win != null)
            {
                win.Open(data);
            }
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="name">窗口名</param>
        public static void CloseByName(PopWinName name)
        {
            if (OpenedWinDic.ContainsKey(name) && OpenedWinDic[name] != null)
            {
                OpenedWinDic[name].Close();
            }
        }

        /// <summary>
        /// 关闭所有窗口
        /// </summary>
        public static void CloseAll()
        {
            foreach (var popWin in OpenedWinDic.Values)
            {
                popWin.Close();
            }
        }

        /// <summary>
        /// 获取窗口实例
        /// </summary>
        /// <param name="name">窗口名</param>
        /// <returns>窗口实例</returns>
        public static PopWin GetPopWinInstance(PopWinName name)
        {
            if (OpenedWinDic.ContainsKey(name))
            {
                return OpenedWinDic[name];
            }
            return null;
        }


        /// <summary>
        /// 窗口是否存在
        /// </summary>
        /// <param name="name">窗口名</param>
        /// <returns>是否存在</returns>
        public static bool IsExist(PopWinName name)
        {
            return OpenedWinDic.ContainsKey(name) && OpenedWinDic[name] != null;
        }

        /// <summary>
        /// 窗口是否打开
        /// </summary>
        /// <param name="name">窗口名</param>
        /// <returns>是否打开</returns>
        public static bool IsOpened(PopWinName name)
        {
            if (IsExist(name) && OpenedWinDic[name] != null)
            {
                return OpenedWinDic[name].IsOpened;
            }
            return false;
        }

        /// <summary>
        /// 初始化窗口
        /// </summary>
        /// <param name="name">窗口名</param>
        /// <returns>实例化的窗口</returns>
        private static PopWin InitPopWin(PopWinName name)
        {
            return null;
        }
    }
}